//
//  YZChatMessageVideoCell.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/6/28.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZChatMessageVideoCell.h"
#import "YZMediaManager.h"
#import "YZVideoManager.h"
#import "UIView+Extension.h"
#import "UIImage+YZExtension.h"

@interface YZChatMessageVideoCell()

@property (nonatomic, strong) UIButton *imageBtn;
@property (nonatomic, strong) UIButton *topBtn;

@end

@implementation YZChatMessageVideoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.imageBtn];
        [self.imageBtn addSubview:self.topBtn];
    }
    return self;
}

- (void)setModelFrame:(YZMessageFrame *)modelFrame{
    [super setModelFrame:modelFrame];
    YZMediaManager *manager = [YZMediaManager shareManager];
    
    /* 转换当前cache目录 + Chat/Video 或 Chat/VideoPic*/
    // 视频
    NSString *videoPath = modelFrame.model.mediaPath;
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    cachePath = [cachePath stringByAppendingPathComponent:kChatVideoPath];
    videoPath = [cachePath stringByAppendingPathComponent:[videoPath lastPathComponent]];
    
    // 视频缩略图
    NSString *videoThumbilPath = modelFrame.model.message.fileKey;
    NSString *cachePath1 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    cachePath1 = [cachePath1 stringByAppendingPathComponent:kVideoPic];
    videoThumbilPath = [cachePath1 stringByAppendingPathComponent:[videoThumbilPath lastPathComponent]];
    
    UIImage *videoArrowImage = nil;
    if (!modelFrame.model.isSender) {
        // 获取缩略图
        videoArrowImage = [UIImage imageWithContentsOfFile:videoThumbilPath];
    } else {
         videoArrowImage = [manager videoConverPhotoWithVideoPath:videoPath size:modelFrame.picViewF.size isSender:modelFrame.model.isSender];
    }
    
    self.imageBtn.frame = modelFrame.picViewF;
    self.bubbleView.userInteractionEnabled = videoArrowImage != nil;
    self.bubbleView.image = nil;
    [self.imageBtn setImage:videoArrowImage forState:UIControlStateNormal];
    self.topBtn.frame = CGRectMake(0, 0, _imageBtn.width, _imageBtn.height);
    
    UIImage *addImage = [UIImage addImage2:[UIImage imageNamed:@"App_video_play_btn_bg"] toImage:videoArrowImage];
    [self.topBtn setImage:addImage forState:UIControlStateNormal];
}

- (void)imageBtnClick:(UIButton *)btn
{
    NSString *path = @"";
    if (!self.modelFrame.model.isSender) {
        path = self.modelFrame.model.message.fileKey;
    } else {
        path = self.modelFrame.model.mediaPath;
    }
    [self videoPlay:path];
}

- (void)videoPlay:(NSString *)path
{
    if (self.videoPlayBlock) {
        self.videoPlayBlock(path);
    }
}

- (void)firstPlay
{
    [self endEditing:YES];
    NSString *path = @"";
    if (!self.modelFrame.model.isSender) {
        path = self.modelFrame.model.mediaPath;
    } else {
        path = self.modelFrame.model.mediaPath;
    }
    [self videoPlay:path];
}

#pragma mark - Getter

- (UIButton *)imageBtn
{
    if (nil == _imageBtn) {
        _imageBtn = [[UIButton alloc] init];
        [_imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _imageBtn.layer.masksToBounds = YES;
        _imageBtn.layer.cornerRadius = 5;
        _imageBtn.clipsToBounds = YES;
    }
    return _imageBtn;
}

- (UIButton *)topBtn
{
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topBtn addTarget:self action:@selector(firstPlay) forControlEvents:UIControlEventTouchUpInside];
        _topBtn.layer.masksToBounds = YES;
        _topBtn.layer.cornerRadius = 5;
    }
    return _topBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
